package com.wiseMen.weixin.user;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.wiseMen.core.dao.EntityDao;
import com.wiseMen.platform.utils.AjaxInfo;
import com.wiseMen.platform.utils.AjaxUtils;
import com.wiseMen.weixin.utils.RequestDataUtils;
import com.wiseMen.weixin.utils.SessionUtils;

@Service("WX_UserService")
public class WxUserService {

	@Autowired
    private EntityDao entityDao;
	
	/**
	 * 根据openId判断是否已注册
	 * @param id
	 * @return true:已注册 false:未注册
	 */
	public boolean isRegister(String id){
		return this.entityDao.selectById(WxUserEntity.class, id)!=null;
	}
	/**
	 * 根据ID查询用户信息
	 * @param id 微信openId
	 * @return
	 * @throws IOException 
	 */
	public WxUserEntity findById(String id){
		WxUserEntity temp = this.entityDao.selectById(WxUserEntity.class, id);
		try {
			if(temp!=null){
				String paw = temp.getPassword();
				temp = login(temp.getUsername(),paw);
				if(temp!=null){
					temp.setId(id);
					temp.setPassword(paw);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return temp;
	}
	
	/**
	 * 登录
	 * @param userName
	 * @param passWord
	 * @return
	 * @throws IOException
	 */
	public WxUserEntity login(String userName,String passWord) throws IOException{
		JSONObject data = RequestDataUtils.post("{method:'login',userName:'"+userName+"',passWord:'"+passWord+"'}");
		WxUserEntity user = null;
		if(data.containsKey("id")){
			user = new WxUserEntity(data);
		}
		return user;
	}
	
	/**
	 * 绑定用户
	 * @param openId
	 * @param userName
	 * @param passWord
	 */
	public void bind(String openId,String userName,String passWord){
		WxUserEntity temp = this.entityDao.selectById(WxUserEntity.class, openId);
		if(temp!=null){
			temp.setId(openId);
			temp.setUsername(userName);
			temp.setPassword(passWord);
			this.entityDao.saveSelective(temp);
		}else{
			temp = new WxUserEntity();
			temp.setId(openId);
			temp.setUsername(userName);
			temp.setPassword(passWord);
			this.entityDao.insert(temp);
		}
	}
	
	
	/**
	 * 个人用户注册
	 * @param openId
	 * @param username
	 * @param password
	 * @param certificateNum
	 * @param email
	 * @throws IOException 
	 */
	public AjaxInfo personalRegister(String openId,String userid,String username,String password,String certificateNum,String email) throws Exception{
		AjaxInfo result = null;
		JSONObject userInfo = new JSONObject();
		if(StringUtils.isNotBlank(userid)){
			userInfo.put("id", userid);
		}
		userInfo.put("username", username);
		userInfo.put("password", password);
		userInfo.put("certificateNum", certificateNum);
		userInfo.put("email", email);
		JSONObject params = new JSONObject();
		params.put("data", new JSONObject[]{userInfo});
		params.put("method", "saveOrUpdatePerson");
		params.put("type", StringUtils.isNotBlank(userid)?"update":"save");
		JSONObject requestData = RequestDataUtils.post(params.toJSONString());
		String state = requestData.getString("state");
		if(StringUtils.equals("suceess", state)){
			this.entityDao.deleteById(WxUserEntity.class, openId);
			WxUserEntity u = new WxUserEntity();
			u.setId(openId);
			u.setUsername(username);
			u.setPassword(password);
			u.setState(0);
			this.entityDao.insert(u);
			u = login(username, password);
			u.setId(openId);
			u.setPassword(password);
			SessionUtils.setUser(u);
			result =  AjaxUtils.renderJsonInfo(true,"注册成功",null);
		}else if(StringUtils.equals("error", state)){
			result =  AjaxUtils.renderJsonInfo(false,requestData.getString("errorinfo"),null);
		}else if(StringUtils.equals("009", state)){
			result =  AjaxUtils.renderJsonInfo(false,"提交表单参数异常，请重新注册",null);
		}else{
			result =  AjaxUtils.renderJsonInfo(false,"未知原因",null);
		}
		return result;
	}
	
	
	/**
	 * 企业用户注册
	 * @param openId
	 * @param username
	 * @param password
	 * @param email
	 * @param orgName
	 * @param orgArea1
	 * @param orgArea2
	 * @param industry1
	 * @param industry2
	 * @param industry3
	 * @param industry4
	 * @return
	 * @throws Exception
	 */
	public AjaxInfo enterpriseRegister(String openId,String userid,String orgId,String username,String password,String email,String orgName
			,String orgArea1,String orgArea2,String industry1,String industry2,String industry3,String industry4) throws Exception{
		AjaxInfo result = null;
		JSONObject userInfo = new JSONObject();
		if(StringUtils.isNotBlank(userid)){
			userInfo.put("id", userid);
		}
		userInfo.put("username", username);
		userInfo.put("password", password);
		userInfo.put("email", email);
		JSONObject userInfo2 = new JSONObject();
		if(StringUtils.isNotBlank(userid)){
			userInfo2.put("id", orgId);
		}
		userInfo2.put("orgName", orgName);
		userInfo2.put("orgArea1", orgArea1);
		userInfo2.put("orgArea2", orgArea2);
		userInfo2.put("industry1", industry1);
		userInfo2.put("industry2", industry2);
		userInfo2.put("industry3", industry3);
		userInfo2.put("industry4", industry4);
		JSONObject params = new JSONObject();
		params.put("data", new JSONObject[]{userInfo,userInfo2});
		params.put("method", "saveOrUpdateEnt");
		params.put("type", StringUtils.isNotBlank(userid)?"update":"save");
		JSONObject requestData = RequestDataUtils.post(params.toJSONString());
		String state = requestData.getString("state");
		if(StringUtils.equals("suceess", state)){
			this.entityDao.deleteById(WxUserEntity.class, openId);
			WxUserEntity u = new WxUserEntity();
			u.setId(openId);
			u.setUsername(username);
			u.setPassword(password);
			u.setState(0);
			this.entityDao.insert(u);
			u = login(username, password);
			u.setId(openId);
			u.setPassword(password);
			SessionUtils.setUser(u);
			result =  AjaxUtils.renderJsonInfo(true,"注册成功",null);
		}else if(StringUtils.equals("error", state)){
			result =  AjaxUtils.renderJsonInfo(false,requestData.getString("errorinfo"),null);
		}else if(StringUtils.equals("009", state)){
			result =  AjaxUtils.renderJsonInfo(false,"提交表单参数异常，请重新注册",null);
		}else{
			result =  AjaxUtils.renderJsonInfo(false,"未知原因",null);
		}
		return result;
	}
}
